var pageBean = null;
var dataList = {};
var updateTimes = 0;
var currentData = null;
var spaces = {};
var clients = {};

$(document).ready(function(e) {
	$("#startTime111").val(getDays());
	$("#endTime222").val(getDays());
	initialSpace();
	initialClientLevle();
	search(1);
	
});
function getDays(){
	var $time= new Date();
	$year = parseInt($time.getFullYear());
	$month = parseInt($time.getMonth())+1;
	$day = parseInt($time.getDate());
	if($month<10){
		$month ='0'+ $month;
	}
	if($day<10){
		$day ='0' + $day;
	}
	return $year +"-"+ $month +"-"+$day;	
}

function resetSearch(){
	$.each($("[searchParameter]"),function(index,element){
		var tagName = $(element).prop("tagName");
		if(tagName == 'SELECT'){
			var firstVal = $(element).find('option').eq(0).val();				
			$(element).val(firstVal).trigger('change');
		}else{
			$(element).val('');
		}
	});
}

function getRequestJson(currentPage){	 
	var like = {};
	var equals = {};
	$.each($("[searchParameter]"),function(index,element){
		var tagName = $(element).prop("tagName");
		var name = $(element).attr("searchParameter");
		var searchType = $(element).attr("searchType");
		if(searchType == 'like'){
			like[name] = $(element).val();
		}else if(searchType == 'equals'){
			equals[name] = $(element).val();
		}
	});
	var requestJson = {currentPage:currentPage,data:{'like':like,'equals':equals}};
	return requestJson;
}

function detail(id){
	var data = dataList[id];	
	if(data == null){
		return ;
	}
	$("DIV[name='dataList']").hide();
	$("DIV[name='viewInfo']").show();	
	
	$("#ViewInfo1").empty();
	var htmlA = '<li><label style="width:150px;">订单编号:</label><label style="width:250px;">'+$.trim(data.orderNumber)+'</label><label style="margin-left:20px;width:110px;">订单类型：</label><label style="width:250px;">打印订单</label></li>';
	htmlA += '<li><label style="width:150px;">订单状态:</label><label style="width:250px;">打印订单</label><label style="margin-left:20px;width:110px;">订单总额：</label><label style="width:250px;">￥'+$.trim(data.totalCost)+'</label></li>';
	htmlA += '<li><label style="width:150px;">打印类型:</label><label style="width:250px;">'+$.trim(data.jobType)+'</label><label style="margin-left:20px;width:110px;">纸张类型：</label><label style="width:250px;">'+$.trim(data.paperSize)+'</label></li>';
	htmlA += '<li><label style="width:150px;">打印方式:</label><label style="width:250px;">'+$.trim(data.duplex)+'</label><label style="margin-left:20px;width:110px;">打印页数：</label><label style="width:250px;">'+$.trim(data.pageCount)+'</label></li>';
	htmlA += '<li><label style="width:150px;">订单创建时间:</label><label style="width:250px;">'+$.trim(data.jobInTime)+'</label><label style="margin-left:20px;width:110px;">空间：</label><label style="width:250px;">'+$.trim(data.spaceName)+'</label></li>';
	htmlA += '<li><label style="width:150px;">打印机名称:</label><label style="width:250px;">'+$.trim(data.printName)+'</label><label style="margin-left:20px;width:110px;">文档名称：</label><label style="width:250px;">'+$.trim(data.documentName)+'</label></li>';
	htmlA += '<li><label style="width:150px;">提交人:</label><label style="width:250px;">'+$.trim(data.memberName)+'</label><label style="margin-left:20px;width:110px;">提交人所属客户：</label><label style="width:250px;">'+$.trim(data.client)+'</label></li>';
	$("#ViewInfo1").html(htmlA);
	
	var detailTable = $("#detailTable");
	$(detailTable).empty();
	var html = '<tr>';
	html += '<td>' +  $.trim(data.pageCount)  + '</td>';
	html += '<td>' + $.trim(data.totalCost/data.pageCount) + '</td>';
	html += '<td>' + $.trim(data.totalCost) + '</td>';    
	html += '</tr>';
	$(detailTable).append(html);
}

/*关闭订单详情页，返回订单列表*/
function closeDetail(){
	$("DIV[name='dataList']").show();
	$("DIV[name='viewInfo']").hide();	
}

function search(currentPage){
	var requestJson = getRequestJson(currentPage); 
	$.ajax({
        url: '/euc/print/query',
        data:JSON.stringify(requestJson),
        beforeSend: function(request) {
        	var accessToken = localStorage.getItem('accessToken');
            request.setRequestHeader("accessToken",accessToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (result, textStatus, response) {
        	console.log(JSON.stringify(result));
        	pageBean = result.data;
        	$("#dataTable").empty();	        	
        	$.each(result.data.list,function(i,data){
        		var html = '<tr>';        		
        		html += '<td>' +  $.trim(data.orderNumber)  + '</td>';
        		html += '<td>' +  $.trim(data.spaceName) + '</td>';
        		html += '<td>' +  $.trim(data.jobType) + '</td>';//类型   打印类型
        		html += '<td>' +  $.trim(data.totalCost) + '</td>';
        		html += '<td>' +  $.trim(data.memberName) + '</td>';
        		html += '<td>' +  $.trim(data.client) + '</td>';
        		html += '<td>' +  $.trim(data.jobInTime) + '</td>';
        		html += '<td><a href="javascript:detail('+data.id+');" class="tablelink">查看</a></td>';
        		html += '</tr>';
	        	$("#dataTable").append(html);
	        	dataList[data.id] = data;
			});
        	$("#totalRow").html(pageBean.totalRow+'&nbsp;');
        	$("#currentPage").html(pageBean.currentPage+'&nbsp;');
        	$("#pageSize").html(pageBean.pageSize+'&nbsp;');
        	var pageLength = 15;
        	initailPageNumber(pageBean.totalPage,pageBean.currentPage,pageLength);        	
        },
        error: function () {
        	console.log('/euc/print/query error');
        }  
    });
}

function initailPageNumber(totalPage,currentPage,pageLength){				
	try{
			var midNumber = Math.ceil((pageLength-2)/2);
			var paginList = $(".paginList");
			$(paginList).empty();			
			var prev = '<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>';
			var next = '<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>';
			if( totalPage <= pageLength - 2){
				$(prev).appendTo($(".paginList"));
				for(var i = 1; i<totalPage+1; i++){
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'+i+'</a></li>';
					if(i == currentPage){
						pagenItem = '<li class="paginItem current"><a href="javascript:;">'+i+'</a></li>';
					}			
					$(pagenItem).appendTo($(".paginList"));
				}
				$(next).appendTo($(".paginList"));
			}else{				
				if(currentPage <= midNumber){
					$(prev).appendTo($(".paginList"));
					for(var i=1;i <= pageLength - 4;i++){
						var pagenItem = '<li class="paginItem"><a href="javascript:;">'+i+'</a></li>';
						if(i == currentPage){
							pagenItem = '<li class="paginItem current"><a href="javascript:;">'+i+'</a></li>';
						}			
						$(pagenItem).appendTo($(".paginList"));
					}		
					var pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'+totalPage+'</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					$(next).appendTo($(".paginList"));
				}if(totalPage - currentPage < midNumber){

					var index = totalPage;
					for(var i=1;i <= pageLength - 4;i++){
						var pagenItem = '<li class="paginItem"><a href="javascript:;">'+index+'</a></li>';
						if(index == currentPage){
							pagenItem = '<li class="paginItem current"><a href="javascript:;">'+index+'</a></li>';
						}			
						$(pagenItem).prependTo($(".paginList"));
						index--;
					}		
					var pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).prependTo($(".paginList"));	
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'+ 1 +'</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					$(prev).prependTo($(".paginList"));
					
					$(next).appendTo($(".paginList"));
				}else if(totalPage - currentPage >= midNumber && currentPage > midNumber){

					pagenItem = '<li class="paginItem current"><a href="javascript:;">'+currentPage+'</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					var count = midNumber - 3;
					for(var i = 1; i<=count;i++){
						pagenItem = '<li class="paginItem"><a href="javascript:;">'+ (currentPage-i)+'</a></li>';
						$(pagenItem).prependTo($(".paginList"));
						pagenItem = '<li class="paginItem"><a href="javascript:;">'+ (currentPage+i)+'</a></li>';
						$(pagenItem).appendTo($(".paginList"));
					}
					pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).prependTo($(".paginList"));	
					pagenItem = '<li class="paginItem"><a href="javascript:;">'+1	+'</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					$(prev).prependTo($(".paginList"));
					
					pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					pagenItem = '<li class="paginItem"><a href="javascript:;">'+totalPage+'</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					$(next).appendTo($(".paginList"));
				}				
			}
			$(".paginItem").click(function(){
				if($(this).hasClass("more")){
					//alert("more");
					return;
				}else if($(this).find(".pagepre").length > 0){
					var cur = $('.paginItem.current').find("a").html();
					cur = parseInt(cur);
					if(cur > 1){
						search(cur - 1);
					}
				}else if($(this).find(".pagenxt").length > 0 ){
					var cur = $('.paginItem.current').find("a").html();
					cur = parseInt(cur);
					if(cur < totalPage){
						search(cur + 1);
					}
				}else{
					var cur = $(this).find("a").html();
					cur = parseInt(cur);
					search(cur);
				}
			});
	}catch(e){/*alert(e.messge);*/}	
}


function initialSpace(){//初始化空间信息
	try{
		var spaceString = localStorage.getItem('space');
		spaces = $.parseJSON(spaceString);
		if(spaceString == null || spaces == null){
			$.ajax({
		        url: '/euc/system/space',
		        data:'',
		        beforeSend: function(request) {
		        	var accessToken = localStorage.getItem('accessToken');
		            request.setRequestHeader("accessToken",accessToken );
		        },
		        dataType: 'JSON',
		        async: true,
		        contentType:'application/json;charset=utf-8',
		        type: 'POST',
		        success: function (result, textStatus, response) {
		        	try{
			        	$.each(result,function(index,data){	
			        		spaces[data.id] = data;  
			   			});
			        	var spaceJson = JSON.stringify(spaces);
			        	localStorage.setItem('space',spaceJson);
			        	initialSpaceSelect();
		        	}catch(e){
		        		console.log(e.message);
		        	}
		        },
		        error: function () {
		        	console.log('/euc/system/space error');
		        }  
		    });
		}else{
			initialSpaceSelect();
		}
	}catch(e){
		console.log(e.messge);
	}
}

function initialSpaceSelect(){
	var searchSpace = $("#searchSpace");	
	$.each(spaces,function(key,space){	
   		$('<option value="'+ space.id +'">'+ $.trim(space.spaceName) +'</option>').appendTo($(searchSpace));
	});
}


function initialClientLevle(){
	$.ajax({
        url: '/euc/client/memberclient',
        data:'',
        beforeSend: function(request) {
        	var accessToken = localStorage.getItem('accessToken');
            request.setRequestHeader("accessToken",accessToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (result, textStatus, response) {
        	console.log(JSON.stringify(result));
        	var client = $("#searchClient");
        	$.each(result,function(index,data){
        		if(data.clientType=="企业"){
        			$('<option value="'+ data.id +'">'+ data.companyName +'</option>').appendTo($(client));
        		}else{
        			$('<option value="'+ data.id +'">'+ data.memberName +'</option>').appendTo($(client));
        		}
        		clients[data.id] = data;
   			});
        },
        error: function () {
        	console.log('/euc/client/memberclient error');
        }  
    });
}

